****************************************************************************************************
****************************************************************************************************
*Replication file for:
*The Importance of a Liberal Power's Attention to Democratic Elections Around the World
*Johannes Bubeck Ashrakat Elshehawy Nikolay Marinov Federico Nanni
****************************************************************************************************
****************************************************************************************************


clear
*use your own working directory
cd "/Data"

*data prepared in step 1
use neldaprep


******sequentially run ttests for each month in the range 1-18, store their results, the R file (9_FigureD.8.R) then plots the stred data 

*Important note - plrase read

*****
*		Figure D8 , (same as Figure 4a) we observe the differences of the means and their confidence intervals
*		Please note the interpretation of these coefficients and confidence intervals should be reversed
*       This is the case as STATA reports the difference between the 0 not close to elections group and the 1 close elections group. One implication is that when bias is greater in the latter group, this difference is negative. Since in the paper we always speak of the difference between the close to elections group and the one away from elections, we reverse the quantity reported by STATA. Thus, one bias close to elections is greater, our reported result is always positive.
*      Results of these ttests below will be used in R using the code called 9_FigureD.8_appendix.R to plot the figure
*****
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein1) 

* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=1
* Export the results to a CSV file
export delimited using resultsfigured8_1.csv, replace


clear
cd "/Data"

use neldaprep

ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein2) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=2
* Export the results to a CSV file
export delimited using resultsfigured8_2.csv, replace



clear
cd "/Data"

use neldaprep

ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein3) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se
clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=3
* Export the results to a CSV file
export delimited using resultsfigured8_3.csv, replace



clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein4) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=4
* Export the results to a CSV file
export delimited using resultsfigured8_4.csv, replace




clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein5) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=5
* Export the results to a CSV file
export delimited using resultsfigured8_5.csv, replace



clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein6) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=6
* Export the results to a CSV file
export delimited using resultsfigured8_6.csv, replace





clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein7) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=7
* Export the results to a CSV file
export delimited using resultsfigured8_7.csv, replace



clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein8)
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=8
* Export the results to a CSV file
export delimited using resultsfigured8_8.csv, replace


clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein9)
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=9
* Export the results to a CSV file
export delimited using resultsfigured8_9.csv, replace




clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein9) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=9
* Export the results to a CSV file
export delimited using resultsfigured8_9.csv, replace



clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein10) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=10
* Export the results to a CSV file
export delimited using resultsfigured8_10.csv, replace



clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein11) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=11
* Export the results to a CSV file
export delimited using resultsfigured8_11.csv, replace



clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein12) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=12
* Export the results to a CSV file
export delimited using resultsfigured8_12.csv, replace



clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein13) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=13
* Export the results to a CSV file
export delimited using resultsfigured8_13.csv, replace



clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein14) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=14
* Export the results to a CSV file
export delimited using resultsfigured8_14.csv, replace



clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein15v1) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=15
* Export the results to a CSV file
export delimited using resultsfigured8_15.csv, replace



clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein16) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=16
* Export the results to a CSV file
export delimited using resultsfigured8_16.csv, replace



clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein17) 
* Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=17
* Export the results to a CSV file
export delimited using resultsfigured8_17.csv, replace



clear
cd "/Data"

use neldaprep
ttest bias if strpos(lower(nelda3),"y")>0 & strpos(lower(nelda4),"y")>0 & strpos(lower(nelda5),"y")>0, by(uspreselein18) 
 * Store the results
return list

* Calculate the confidence intervals
scalar mean_diff =  r(mu_2)-r(mu_1) 
scalar se = r(se)
scalar  ci_lower= mean_diff - 1.96*se
scalar  ci_upper = mean_diff + 1.96*se

clear
set obs 1
gen mean_difference = mean_diff
gen lower_confidence = ci_lower
gen upper_confidence = ci_upper
gen election="Pres"
gen months=18
* Export the results to a CSV file
export delimited using resultsfigured8_18.csv, replace


*use 9_FigureD.8_appendix.R to plot these estimates 

